<template>
  <div id="app">
    <el-container>
      <el-row>
        <el-input v-model="code" placeholder="输入监控点编码" @change="onChangeCode"></el-input>
        <el-button @click="onSubmit">默认预览</el-button>
        <el-button @click="onTwoSubmit(2)">4分屏</el-button>
        <el-button @click="onTwoSubmit(4)">16分屏</el-button>
        <el-button @click="onTwoSubmit(8)">64分屏</el-button>
      </el-row>
      <el-row>
        <div id="player" style="width: 800px;height: 800px;"></div>
      </el-row>
    </el-container>
  </div>
</template>

<script>
import http from '@/http/http2.js'
import '@/static/util/h5player.min.js'

export default {
  name: 'hik',
  data () {
    return {
      // 监控点编码
      code: '',
      // 播放器对象
      player: null
    }
  },
  mounted () {
    this.initPlayer()
  },
  methods: {

    /**
     * 初始化播放器
     */
    initPlayer () {
      this.player = new window.JSPlugin({
        // 需要英文字母开头 必填
        szId: 'player',
        // 必填,引用H5player.min.js的js相对路径
        szBasePath: './static/util',

        // 当容器div#play_window有固定宽高时，可不传iWidth和iHeight，窗口大小将自适应容器宽高
        iWidth: 600,
        iHeight: 400,

        // 分屏播放，默认最大分屏4*4
        iMaxSplit: 16,
        iCurrentSplit: 1,

        // 样式
        oStyle: {
          border: '#343434',
          borderSelect: '#FFCC00',
          background: '#000'
        }
      })
    },

    /**
     * 获取取流连接
     * @returns {*}
     */
    getPreviewUrl () {
      // let tempCode = '61077001001320000018'
      let tempCode = '61011011101320000004'
      if (this.code) {
        tempCode = this.code
      }
      const param = {
        'cameraIndexCode': tempCode,
        'streamType': 0,
        'protocol': 'ws',
        'transmode': 1
      }
      return http.post('/ay-video-manage/video/monitor/getRealTimeMonitorVideo', param)
    },

    /**
     * 播放
     */
    play (index) {
      const _this = this
      this.getPreviewUrl().then(res => {
        if (res.status !== 200) {
          _this.$message.warning('获取视频流失败！')
          return
        }
        let preUrl = res.data.data.url
        const param = {
          playURL: preUrl,
          // 1：高级模式  0：普通模式，高级模式支持所以
          mode: 1
        }
        // 索引默认0
        if (!index) {
          index = 0
        }
        _this.player.JS_Play(preUrl, param, index).then(() => {
          console.log('播放成功')
        },
        (err) => {
          console.log('播放失败')
        })
      })
    },

    /**
     * 监控点更新
     * @param data
     */
    onChangeCode (data) {
      this.code = data
      this.play()
    },
    /**
     * 默认预览
     */
    onSubmit () {
      this.play()
    },

    /**
     * 分屏
     */
    onTwoSubmit (num) {
      const _this = this
      this.player.JS_ArrangeWindow(num).then(
        () => {
          // 循环取流
          for (let i = 0; i < num * num; i++) {
            _this.play(i)
          }
        },
        e => { console.error(e) }
      )
    }
  }
}
</script>

<style scoped>

</style>
